package ustc.xiaoyang.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtils {
    //Base64编码的密钥
    private static final String signKey = "QXR0aWN1c1lhbmcK";
    //JWT过期时间，单位：毫秒
    private static final long expire = 1000 * 60 * 60 * 12;

    /**
     * 生成JWT
     * @param claims
     * @return
     */
    public static String generateJwt(Map<String,Object> claims){
        return Jwts.builder()
                    .addClaims(claims)
                    .signWith(SignatureAlgorithm.HS256,signKey)
                    .setExpiration(new Date(System.currentTimeMillis() + expire))
                    .compact();

    }

    /**
     * 解析JWT
     * @param jwt
     * @return
     */
    public static Claims parseJwt(String jwt){
        return Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
    }
}
